﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {

            }
            else
            {
                FillData();
            }
        }
        #endregion

        #region 数据传入
        private void FillData()
        {
            var sql = "select * from Articles";

            var dt = DbHelper.GetDatatable(sql);

            GV1.DataSource = dt;

            GV1.DataBind();
        }
        #endregion

        #region 获取数据库行与列
        public string GetCellValue(int rowIndex, int colIndex)
        {

            var control = GV1.Rows[rowIndex].Cells[colIndex];

            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;

            return res;

        }
        #endregion

        #region 添加按钮
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var sql = "select * from Articles";

            var dt = DbHelper.GetDatatable(sql);

            dt.Rows.Add(dt.NewRow());

            GV1.DataSource = dt;

            GV1.DataBind();

            GV1.EditIndex = dt.Rows.Count - 1;

            GV1.DataSource = dt;

            GV1.DataBind();

        }
        #endregion

        #region 删除按钮
        protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            var count = GV1.Rows.Count;
 
            var id = GetCellValue(e.RowIndex, 0);

            if (count > 0)
            {
                string sql = string.Format("delete from Articles where id = '{0}'", id);

                var deleteSQL = DbHelper.AddOrDeletedOrUpdated(sql);

                FillData();
            }
        }
        #endregion

        #region 更新按钮
        protected void GV1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var isString = GetCellValue(e.RowIndex, 0);
            var Title = GetCellValue(e.RowIndex, 1);
            var Content = GetCellValue(e.RowIndex, 2);
            var Author = GetCellValue(e.RowIndex, 3);
            var isok = int.TryParse(isString, out int id);
            if (isok)
            {
                string updateSql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}' where id = '{3}'", Title, Content, Author,id);

                var dt = DbHelper.AddOrDeletedOrUpdated(updateSql);

                GV1.EditIndex = -1;

                FillData();
            }
            else
            {
                string insertSql = string.Format("insert into Articles (Title,Content,Author) values ('{0}','{1}','{2}')", Title, Content, Author);

                var dt = DbHelper.AddOrDeletedOrUpdated(insertSql);

                FillData();
            }
        }
        #endregion

        #region 编辑按钮
        protected void GV1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV1.EditIndex = e.NewEditIndex;
            FillData();
        }
        #endregion

        #region 取消编辑
        protected void GV1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV1.EditIndex = -1;
            FillData();
        }
        #endregion


    }
}